package com.qztech.sdk.ui;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.res.Resources;
import android.view.View;

import com.qztech.sdk.R;

import java.lang.reflect.Field;
public class DialogHelper {

    public static void alert(Context ctx, CharSequence title,
                             CharSequence message, CharSequence okText,
                             OnClickListener oklistener) {
        Builder builder = createDialog(ctx, title, message);
        builder.setPositiveButton(okText, oklistener);
        builder.setCancelable(false).create().show();
    }

    public static void alert(Context ctx, int titleId, int messageId,
                             int okTextId, OnClickListener oklistener) {
        alert(ctx, ctx.getText(titleId), ctx.getText(messageId),
                ctx.getText(okTextId), oklistener);
    }

    public static void confirm(Context ctx, CharSequence title,
                               CharSequence message, CharSequence okText,
                               OnClickListener oklistener, CharSequence cancelText,
                               OnClickListener cancellistener) {
        Builder builder = createDialog(ctx, title, message);
        builder.setPositiveButton(okText, oklistener).setNegativeButton(cancelText, cancellistener).setCancelable(false).create().show();
    }

    public static void confirm(Context ctx, View view, CharSequence title,
                               CharSequence message, CharSequence okText,
                               OnClickListener oklistener, CharSequence cancelText,
                               OnClickListener cancellistener) {
        Builder builder = new Builder(ctx);
        builder.setView(view).setTitle(title).setPositiveButton(okText, oklistener).setNegativeButton(cancelText, cancellistener).setCancelable(false).create().show();
    }

    /**
     * 确认删除
     */
    public static void confirm(Context ctx, CharSequence title,
                               CharSequence message, CharSequence okText,
                               OnClickListener oklistener) {
        Builder builder = createDialog(ctx, title, message);
        builder.setPositiveButton(okText, oklistener);
        builder.setCancelable(false).create().show();
    }

    public static void confirm(Context ctx, int titleId, int messageId,
                               int okTextId, OnClickListener oklistener) {
        confirm(ctx, ctx.getText(titleId), ctx.getText(messageId),
                ctx.getText(okTextId), oklistener);
    }

    public static void confirm(Context ctx, int titleId, int messageId,
                               int okTextId, int cancelId, OnClickListener oklistener) {
        confirm(ctx, ctx.getText(titleId), ctx.getText(messageId),
                ctx.getText(okTextId), oklistener, ctx.getText(cancelId), null);
    }

    public static void confirm(Context ctx, View view, int titleId, int messageId,
                               int okTextId, int cancelId, OnClickListener oklistener) {
        confirm(ctx, view, ctx.getText(titleId), ctx.getText(messageId),
                ctx.getText(okTextId), oklistener, ctx.getText(cancelId), null);
    }

    public static void confirm(Context ctx, int titleId, int messageId,
                               int okTextId, OnClickListener oklistener, int cancelTextId,
                               OnClickListener cancellistener) {
        confirm(ctx, ctx.getText(titleId), ctx.getText(messageId),
                ctx.getText(okTextId), oklistener, ctx.getText(cancelTextId),
                cancellistener);
    }

    private static Builder createDialog(Context ctx,
                                        CharSequence title, CharSequence message) {
        Builder builder = new Builder(ctx);
        builder.setMessage(message);
        if (title != null) {
            builder.setTitle(title);
        }
        return builder;
    }

    public static void displayWarning(Context ctx, String message) {
        if (ctx == null)
            return;
        final Resources res = ctx.getResources();
        Builder b = new Builder(ctx);
        b.setTitle(res.getString(R.string.notice)).setCancelable(false);
        b.setMessage(message);
        b.setPositiveButton(res.getString(R.string.ok), new OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        b.show();
    }

    public static Builder displayWarning(Context mContext, int resourceId) {
        if (mContext == null)
            return null;
        final Resources res = mContext.getResources();
        Builder b = new Builder(mContext);
        b.setTitle(res.getString(R.string.notice)).setCancelable(false);
        b.setMessage(resourceId);
        b.setPositiveButton(res.getString(R.string.ok), new OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        b.show();
        return b;
    }

    public static Builder displayWarning(Context mContext, int resourceId,
                                         OnClickListener listener) {
        if (mContext == null)
            return null;
        final Resources res = mContext.getResources();
        Builder b = new Builder(mContext);
        b.setTitle(res.getString(R.string.notice)).setCancelable(false);
        b.setMessage(resourceId);
        b.setPositiveButton(res.getString(R.string.ok), listener);
        b.show();
        return b;
    }

    public static Builder createDialog(Context ctx, int titleId,
                                       int messageId) {
        Builder builder = new Builder(ctx);
        builder.setMessage(messageId);
        builder.setTitle(titleId);
        return builder;
    }

    public static void ViewDialog(Context ctx, CharSequence title, View view,
                                  CharSequence okText, OnClickListener oklistener,
                                  CharSequence cancelText, OnClickListener cancellistener) {

    }

    public static void ViewDialog(Context ctx, int titleId, View view,
                                  int okTextId, OnClickListener oklistener, int cancelTextId,
                                  OnClickListener cancellistener) {

        ViewDialog(ctx, ctx.getText(titleId), view, ctx.getText(okTextId),
                oklistener, ctx.getText(cancelTextId), cancellistener);

    }

    public static void SetDialogShowing(DialogInterface dialog, boolean showing) {
        try {
            Field field = dialog.getClass().getSuperclass()
                    .getDeclaredField("mShowing");
            field.setAccessible(true);
            field.set(dialog, showing);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static ProgressDialog buildWaitingDialog(Activity activity,
                                                    String title, String message, boolean isCal) {
        ProgressDialog waitingDialog = ProgressDialog.show(activity, title,
                message);
        waitingDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置风格为圆形进度条
        waitingDialog.setIndeterminate(false);// 设置进度条是否为不明确
        waitingDialog.setCancelable(isCal);// 设置进度条是否可以按退回键取消
        waitingDialog.show();
        return waitingDialog;
    }

    public static ProgressDialog buildWaitingDialog(Activity activity,
                                                    int title, int message, boolean isCal) {
        ProgressDialog waitingDialog = ProgressDialog.show(activity, activity
                .getResources().getString(title), activity.getResources()
                .getString(message));
        waitingDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);// 设置风格为圆形进度条
        waitingDialog.setIndeterminate(false);// 设置进度条是否为不明确
        waitingDialog.setCancelable(isCal);// 设置进度条是否可以按退回键取消
        waitingDialog.show();
        return waitingDialog;
    }

    public static void closeProgressDialog(ProgressDialog dialog) {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
        dialog = null;
    }

}